home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Extra 1996 #3 / AmigaPlus_CD-ROM-EXTRA_Nr.3.bin / aminet-spiele / role on / larn / nap.c < prev    next >
C/C++ Source or Header  |  1997-12-31  |  4KB  |  179 lines

  1. /* nap.c */
  2. #ifdef VMS
  3. #include <signal.h>
  4. #include <types.h>
  5. #include <time.h>
  6. #else
  7. #include <signal.h>
  8. #include <sys/types.h>
  9. #ifdef SYSV
  10. # ifdef MSDOS
  11. #  ifdef    OS2LARN
  12. #   define   INCL_BASE
  13. #   include <os2.h>
  14. #   define sleep(x)    DosSleep(x*1000L)
  15. #  endif
  16. #   include <dos.h>
  17. # else
  18. #   include <sys/times.h>
  19. # endif
  20. #else
  21. #ifdef BSD
  22. #include <sys/timeb.h>
  23. #endif BSD
  24. #endif SYSV
  25. #endif
  26. /*
  27.  *  routine to take a nap for n milliseconds
  28.  */
  29. nap(x)
  30.     register int x;
  31.     {
  32.     if (x<=0) return; /* eliminate chance for infinite loop */
  33.     lflush();
  34.     if (x > 999) sleep(x/1000); else napms(x);
  35.     }
  36.  
  37. #ifdef NONAP
  38. static napms(x)    /* do nothing */
  39.     int x;
  40.     {
  41.     }
  42. #else NONAP
  43. #ifdef SYSV
  44. /*  napms - sleep for time milliseconds - uses times() */
  45. /* this assumes that times returns a relative time in 60ths of a second */
  46. /* this will do horrible things if your times() returns seconds! */
  47.  
  48. #ifdef MSDOS
  49. unsigned long
  50. static dosgetms()
  51. {
  52. #ifdef    OS2LARN
  53.   DATETIME dt;
  54.   DosGetDateTime(&dt);
  55.  
  56.   /* return hundreths of seconds */
  57.   return ( 360000L * dt.hours   +
  58.        6000L   * dt.minutes +
  59.        100L    * dt.seconds + dt.hundredths );
  60. #else
  61.     union REGS regs;
  62.  
  63.     regs.h.ah = 0x2C;
  64.     intdos(®s, ®s);
  65.  
  66.     /* return hundreths of seconds
  67.     */
  68.     return ( 360000L * regs.h.ch +
  69.            6000L * regs.h.cl +
  70.         100L * regs.h.dh + regs.h.dl );
  71. #endif
  72. }
  73.  
  74. static napms(time)
  75. int time;
  76. {
  77.     unsigned long matchclock;
  78.  
  79.     if (time <= 0)
  80.         time = 1;   /* eliminate chance of infinite loop */
  81.     matchclock = dosgetms() + (time + 5) / 10;
  82.     if (matchclock > 8640000L)   /* total 100ths-of-seconds in 24 hrs */
  83.         return;
  84.  
  85.     while (matchclock > dosgetms())
  86.         ;
  87. }
  88.  
  89. # else
  90. static napms(time)
  91.     int time;
  92.     {
  93.     long matchclock, times();
  94.     struct tms stats;
  95.  
  96.     if (time<=0) time=1; /* eliminate chance for infinite loop */
  97.     if ((matchclock = times(&stats)) == -1 || matchclock == 0)
  98.         return; /* error, or BSD style times() */
  99.     matchclock += (time / 17);      /*17 ms/tic is 1000 ms/sec / 60 tics/sec */
  100.  
  101.     while(matchclock < times(&stats))
  102.         ;
  103.     }
  104. # endif /* MSDOS */
  105.  
  106. #else not SYSV
  107. #ifdef BSD
  108. #ifdef SIGVTALRM
  109. /* This must be BSD 4.2!  */
  110. #include <sys/time.h>
  111. #define bit(_a) (1<<((_a)-1))
  112.  
  113. static void nullf()
  114.     {
  115.     }
  116.  
  117. /*  napms - sleep for time milliseconds - uses setitimer() */
  118. static napms(time)
  119.     int time;
  120.     {
  121.     struct itimerval    timeout;
  122. #ifdef SIG_RTNS_INT
  123.     int     (*oldhandler) ();
  124. #else
  125.     void     (*oldhandler) ();
  126. #endif
  127.     int     oldsig;
  128.  
  129.     if (time <= 0) return;
  130.  
  131.     timerclear(&timeout.it_interval);
  132.     timeout.it_value.tv_sec = time / 1000;
  133.     timeout.it_value.tv_usec = (time % 1000) * 1000;
  134.  
  135.     oldsig = sigblock(bit(SIGALRM));
  136.     setitimer(ITIMER_REAL, &timeout, (struct itimerval *)0);
  137.     oldhandler = signal(SIGALRM, nullf);
  138.     sigpause(oldsig);
  139.     signal(SIGALRM, oldhandler);
  140.     sigsetmask(oldsig);
  141.     }
  142.  
  143. #else
  144. /*  napms - sleep for time milliseconds - uses ftime() */
  145.  
  146. static napms(time)
  147.     int time;
  148.     {
  149.     /* assumed to be BSD UNIX */
  150.     struct timeb _gtime;
  151.     time_t matchtime;
  152.     unsigned short matchmilli;
  153.     register struct timeb *tp = & _gtime;
  154.  
  155.     if (time <= 0) return;
  156.     ftime(tp);
  157.     matchmilli = tp->millitm + time;
  158.     matchtime  = tp->time;
  159.     while (matchmilli >= 1000)
  160.         {
  161.         ++matchtime;
  162.         matchmilli -= 1000;
  163.         }
  164.  
  165.     while(1)
  166.         {
  167.         ftime(tp);
  168.         if ((tp->time > matchtime) ||
  169.             ((tp->time == matchtime) && (tp->millitm >= matchmilli)))
  170.             break;
  171.         }
  172.     }
  173. #endif
  174. #else not BSD
  175. static napms(time) int time; {} /* do nothing, forget it */
  176. #endif BSD
  177. #endif SYSV
  178. #endif NONAP
  179.